import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_device_curve_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/device_manger/curve/model.dart';

class WarehouseDeviceCurveController extends GetxController {
  var timeType = WarehouseMonitorCurveTimeType.week;
  var devType = WarehouseMonitorCurveType.temperature;

  late final args = Get.arguments as Map<String, dynamic>;

  /// 温度
  final tempCurve = <ChartData>[];

  /// 湿度
  final humidityCurve = <ChartData>[];
  List<ChartData> get data => devType == WarehouseMonitorCurveType.temperature ? tempCurve : humidityCurve;

  /// 获取曲线数据
  Future<void> getCurveData() async {
    tempCurve.clear();
    humidityCurve.clear();
    update();
    
    final resp = await HttpUtil().generateGetRequest<WarehouseDeviceCurveEntity>(
      '${Interface.mainBaseUrl}/storage//device/getDeviceHisStaticVo',
      queryParameters: {
        'type': timeType.index + 1,
        'mac': args['mac'],
      },
    ).execute();

    resp.onSuccess((result) {
      if (result.data != null) {
        for (int idx = 0; idx < result.data!.x!.length; idx++) {
          final time = result.data!.x![idx];
          final temp = result.data!.temperatureY![idx];
          final humidity = result.data!.humidityY![idx];
          tempCurve.add(ChartData(time, temp, Colors.white));
          humidityCurve.add(ChartData(time, humidity, Colors.white));
        }
        update();
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
    getCurveData();
  }
}
